package org.dromara.common.excel.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelToMarkdown {
    public static void main(String[] args) throws IOException {
        // Excel 文件路径
        String excelFilePath = "D:\\files\\大连科技学院个人课表.xlsx";

        // 打开 Excel 文件
        FileInputStream fis = new FileInputStream(excelFilePath);
        Workbook workbook = new XSSFWorkbook(fis);

        // 遍历每个 sheet
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            Sheet sheet = workbook.getSheetAt(i);
            System.out.println("## Sheet: " + sheet.getSheetName());

            StringBuilder sb = new StringBuilder();

            // 遍历行
            for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
                Row row = sheet.getRow(rowIndex);
                if (row == null) continue;

                sb.append("|");
                for (int colIndex = 0; colIndex < row.getLastCellNum(); colIndex++) {
                    Cell cell = row.getCell(colIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    sb.append(cell.toString()).append("|");
                }
                sb.append("\n");

                // 在首行后加 Markdown 表头分隔行
                if (rowIndex == 0) {
                    sb.append("|");
                    for (int colIndex = 0; colIndex < row.getLastCellNum(); colIndex++) {
                        sb.append("---|");
                    }
                    sb.append("\n");
                }
            }

            // 打印 markdown 结果
            System.out.println("```markdown");
            System.out.println(sb.toString());
            System.out.println("```");
        }

        workbook.close();
        fis.close();
    }
}
